#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 40005;

int n, m;
int a[N], b[N], num;
int l[205], r[205], bid[N];
int t[N], cnt[205][N];
int mode[205][205];

void init() {
  // discrete
  rep(i, 1, n) b[i] = a[i];
  sort(b + 1, b + n + 1);
  num = unique(b + 1, b + n + 1) - b - 1;
  rep(i, 1, n) a[i] = lower_bound(b + 1, b + num + 1, a[i]) - b;

  //
  int k = sqrt(n);
  rep(i, 1, n) bid[i] = (i - 1) / k + 1;
  int blk = bid[n];
  rep(i, 1, blk) {
    l[i] = (i - 1) * k + 1;
    r[i] = i * k;
  }
  r[blk] = n;

  //
  rep(i, 1, blk) {
    memset(t, 0, sizeof(t));
    int mx = 0;
    rep(j, i, blk) {
      rep(p, l[j], r[j]) {
        ++t[a[p]];
        if (t[a[p]] > t[mx] || t[a[p]] == t[mx] && a[p] < mx) mx = a[p];
      }
      mode[i][j] = mx;
    }
  }
  rep(i, 1, blk) {
    rep(j, 1, num) { cnt[i][j] = cnt[i - 1][j]; }
    rep(j, l[i], r[i]) cnt[i][a[j]]++;
  }
}
int query(int L, int R) {
  int id1 = bid[L], id2 = bid[R];
  if (id1 == id2) {
    rep(i, L, R) t[a[i]] = 0;
    int mx = 0;
    rep(i, L, R) {
      t[a[i]]++;
      if (t[a[i]] > t[mx] || t[a[i]] == t[mx] && a[i] < mx) mx = a[i];
    }
    return b[mx];
  } else {
    set<int> cand;
    rep(i, L, r[id1]) cand.insert(a[i]);
    if (id1 + 1 <= id2 - 1) cand.insert(mode[id1 + 1][id2 - 1]);
    rep(i, l[id2], R) cand.insert(a[i]);
    for (int x : cand) t[x] = 0;
    int mx = 0;
    if (id1 + 1 <= id2 - 1) {
      mx = mode[id1 + 1][id2 - 1];
      for (int x : cand) t[x] = cnt[id2 - 1][x] - cnt[id1][x];
    }
    rep(i, L, r[id1]) {
      t[a[i]]++;
      if (t[a[i]] > t[mx] || t[a[i]] == t[mx] && a[i] < mx) mx = a[i];
    }
    rep(i, l[id2], R) {
      t[a[i]]++;
      if (t[a[i]] > t[mx] || t[a[i]] == t[mx] && a[i] < mx) mx = a[i];
    }
    return b[mx];
  }
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  rep(i, 1, n) cin >> a[i];
  init();

  int x = 0;
  while (m--) {
    int l, r;
    cin >> l >> r;
    l = (l + x - 1) % n + 1, r = (r + x - 1) % n + 1;
    if (l > r) swap(l, r);
    x = query(l, r);
    cout << x << endl;
  }
  return 0;
}